class code2 {
    //如果存过这个数，那么存在循环
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while(n!=1 && !set.contains(n)) {
            set.add(n);
            n = Happy(n);
        }
        return n==1;
    }
    public int Happy(int n) {
        int ret = 0;
        while(n!=0) {
            int x = n % 10;
            n /= 10;
            ret += Math.pow(x,2);
        }
        return ret;
    }
}